﻿using System;
using System.Collections.Generic;

namespace Extension {
  public static class IntExt {
    /// <summary>
    /// 返回一个指定的迭代器序列
    /// </summary>
    /// <param name="start">开始的数</param>
    /// <param name="end">结束数</param>
    /// <param name="step">步长</param>
    /// <returns></returns>
    public static IEnumerable<int> StepTo(this int start, int end, int step = 1) {
      if(start > end) throw new ArgumentException($"Argument error: {start} not less than {end}!");

      int result = start - step;
      while(true) {
        result += step;
        if(result >= end) yield break;

        yield return result;
      }
    }
    /// <summary>
    /// 获取一个整型数组序列
    /// </summary>
    /// <param name="endNumber">结束数字</param>
    /// <param name="step">步长</param>
    /// <returns></returns>
    public static int[] Range(int endNumber, int step = 1) {
      var length = endNumber % step == 0 ? endNumber / step : endNumber / step + 1;
      int[] arr = new int[length];
      arr[0] = 0;
      for(int i = 1; i < length; i++) {
        arr[i] = arr[i - 1] + step;
      }
      return arr;
    }
    /// <summary>
    /// 获取一个整型数组序列
    /// </summary>
    /// <param name="start">开始数</param>
    /// <param name="end">结束数</param>
    /// <param name="step">步长</param>
    /// <returns></returns>
    public static int[] Range(int start, int end, int step = 1) {
      var width = end - start;
      if(width <= 0) throw new ArgumentException($"Argument Error：{start} not less than {end} !");

      var length = width % step == 0 ? width / step : width / step + 1;
      int[] arr = new int[length];
      arr[0] = start;
      for(int i = 1; i < length; i++) {
        arr[i] = arr[i - 1] + step;
      }
      return arr;
    }
  }
}
